import cv2 as cv
from python_ai.common.xcommon import *
import numpy as np
import os
import sys
import datetime
import matplotlib.pyplot as plt

spr = 1
spc = 3
spn = 0
plt.figure(figsize=[12, 6])


def show_img(title, img, trans=None, **kwargs):
    global spn
    spn += 1
    if spn > spr * spc:
        return
    plt.subplot(spr, spc, spn)
    plt.axis('off')
    if trans is not None:
        img = trans(img)
    plt.imshow(img, **kwargs)


# load
img = cv.imread('../../../../large_data/CV2/lesson/Day03/131.jpg', cv.IMREAD_COLOR)
img_shape = img.shape
IMG_H = img_shape[0]
IMG_W = img_shape[1]
show_img('ori', img, lambda x: cv.cvtColor(x, cv.COLOR_BGR2RGB))

# tick
pt1 = (2177, 2713)  # left top
pt2 = (393, 2657)  # right top
pt3 = (2217, 401)  # left bottom
cv.circle(img, pt1, 50, (0, 255, 0), -1)
cv.circle(img, pt2, 50, (0, 255, 0), -1)
cv.circle(img, pt3, 50, (0, 255, 0), -1)
show_img('Ticked', img, lambda x: cv.cvtColor(x, cv.COLOR_BGR2RGB))

TOP_PADDING = IMG_H // 10
pt1a = (0, TOP_PADDING)
pt2a = (IMG_W, TOP_PADDING)
pt3a = (0, IMG_H)
pts_ori = np.float32((pt1, pt2, pt3))  # ATTENTION CV_32F is needed
pts_affined = np.float32((pt1a, pt2a, pt3a))
M = cv.getAffineTransform(pts_ori, pts_affined)
print(M)
img_affined = cv.warpAffine(img, M, (IMG_W, IMG_H))
show_img('Affined', img_affined, lambda x: cv.cvtColor(x, cv.COLOR_BGR2RGB))
